package moodish.client;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import moodish.client.gui.MoodishClientGui;
import moodish.client.gui.utils.SortFriends;
import moodish.client.user.User;
import moodish.comm.ClientComm;
import moodish.comm.ClientSideMessage;


public class ClientSideCommManeger {

	/**
	 * @param comm
	 * @param gui
	 * @throws UnknownHostException
	 * @throws IOException
	 */
	public void connectToServer(ClientComm comm, MoodishClientGui gui) throws UnknownHostException, IOException{
		User user = new User(gui.getSimpleLogin().getNickNameField().getText());
		if(user.getNickName().equals(""))
			JOptionPane.showMessageDialog(null, "Please insert a nickName before enter in the Moodish Network");
		if(gui.getMoodishMessage().recived().equals("error")){
			JOptionPane.showConfirmDialog(null, "Server Error/Perhaps selected nickName is connected already");
		}
		if(user.getNickName() != null && !user.getNickName().equals("")){
			gui.setUser(user);

			comm.connect(ClientSideMessage.SERVER, gui.getUser().getNickName());
			gui.gotoMainPane();
			gui.setHendler(new MessageHendler(comm, gui));
			gui.getHendler().start();
		}
	}

	/**
	 * @param comm
	 * @param gui
	 */
	public void sendFriendShipRequest(ClientComm comm, MoodishClientGui gui){
		String nickName = null;
		User user = null;
		if(gui.getMainPanel().getReqFriendField().getText() != null){
			nickName = gui.getMainPanel().getReqFriendField().getText();
			user = new User(nickName);
		}
		
		if(user != null && user.getNickName() != null){
			comm.friendship(user.getNickName());
			gui.getUser().getFriendmodel().addFriendToTable(user);
		}
		
	}
	/**
	 * @param comm
	 * @param gui
	 */
	public void sendUnfriendShipRequest(ClientComm comm, MoodishClientGui gui){
		String nickName = null;
		User user = null;
		
		if(gui.getMainPanel().getReqUnFriendField() != null){
			nickName = gui.getMainPanel().getReqUnFriendField().getText();
			user = new User(nickName);
		}
		
		if(user != null && user.getNickName() != null){
			comm.unfriendship(user.getNickName());
			gui.getUser().getFriendmodel().removeFriend(user.getNickName());
		}
	}

	/**
	 * @param comm
	 * @param gui
	 */
	public void sendMoodishMessage(ClientComm comm, MoodishClientGui gui){
		if(gui.getMainPanel().getMoodToSend() != null){
			comm.sendMoodishMessage(gui.getMainPanel().getMoodToSend());
			System.out.println(gui.getMainPanel().getMoodToSend());
		}
	}

	/**
	 * @param comm
	 * @param gui
	 */
	public void disconnectClient(ClientComm comm, MoodishClientGui gui){
		comm.disconnect();
		gui.changeView(gui.getSimpleLogin());
	}

	/**
	 * @param mood
	 * @param gui
	 */
	public void searchFriends(String mood, MoodishClientGui gui) {
		gui.getMainPanel().getModel().getFriendList().removeAll(gui.getMainPanel().getModel().getFriendList());

		ArrayList<User> list = new ArrayList<User>();
		list.addAll(SortFriends.usersWithMood(mood, gui.getUser().getFriendmodel().getFriendList()));
		for (User user : list) {
			gui.getMainPanel().getModel().addFriendToTable(user);
		}
	}
}
